package com.supermarket_project.service.impl;
import com.supermarket_project.mapper.XiaoshouMapper;
import com.supermarket_project.pojo.subject.*;
import com.supermarket_project.service.XiaoshouService;
import com.supermarket_project.utils.Datevutils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ljc
 * Date: 2022/07/07 9:19
 * Description:
 * Version: V1.0
 */
@Service
public class XIaoshouServiceImpl implements XiaoshouService {
    @Autowired
    XiaoshouMapper xiaoshouMapper;
    @Override
    public List<Staffvo> getAllstaff_info(Staffvo staffvo) {
        int id= staffvo.getStaffId();
        Date date = staffvo.getTime();
        String year = new Datevutils(date).getYear();
        String month = new Datevutils(date).getMon();
        return xiaoshouMapper.getAllstaffInfo_m( id,year,month);

    }
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1查询出所有一级分类 parent_id=0
        List<Goodsvo> oneSubjectList = xiaoshouMapper.oneSubjectList();
        //2查询出所有二级分类 parent_id!=0
        List<Goodsvo> twoSubjectList = xiaoshouMapper.twoSubjectList();
        //3封装一级分类
        List<OneSubject> finnalList = new ArrayList<>();
        for (Goodsvo goods1 : oneSubjectList) {
            //new OneSubject设置值，add加入list
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            //复制操作
            BeanUtils.copyProperties(goods1, oneSubject);
            finnalList.add(oneSubject);
            //4封装二级分类
            //创建list集合封装每一个一级分类的二级分类
            ArrayList<TwoSubject> twoFinnalList = new ArrayList<>();
            for (Goodsvo goods2 : twoSubjectList) {
                if (goods1.getCategoryId().equals(goods2.getFatherId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    //如过一级分类的id==二级分类的parent_id,进行封装
                    BeanUtils.copyProperties(goods2, twoSubject);
                    twoFinnalList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinnalList);
        }
        return finnalList;
    }

    @Override
    public List<Goodsvo> getgoodsinfo(Goodsvo goodsvo) {
        Integer categoryId = goodsvo.getCategoryId();
        Integer fatherId= goodsvo.getFatherId();
        Date time = goodsvo.getTime();
        String month = new Datevutils(time).getMon();
        String year = new Datevutils(time).getYear();

        return xiaoshouMapper.finallselect(categoryId,fatherId,year,month);
    }
}

